package com.renrenyou.duanshipin.net;

import androidx.annotation.NonNull;

import com.google.gson.Gson;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class MyOkhttp {

    private final OkHttpClient mClient;
    public static final int DOWN_STATUS_NONE = 0;//没有下载任务
    public static final int DOWN_STATUS_ING = 1;//有下载任务
    private int mDownloadStatus = DOWN_STATUS_NONE;//下载状态

    public static MyOkhttp getInstance() {
        return SingletonHolder.instance;
    }

    private static class SingletonHolder {
        private static final MyOkhttp instance = new MyOkhttp();
    }

    private MyOkhttp() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(30, TimeUnit.SECONDS);
        builder.readTimeout(30, TimeUnit.SECONDS);
        builder.addInterceptor(new LogInterceptor());
        mClient = builder.build();
    }

    public int getDownloadStatus() {
        return mDownloadStatus;
    }

    public void setDownloadStatus(int downloadStatus) {
        this.mDownloadStatus = downloadStatus;
    }

    public Call get(@NonNull String url, @NonNull Callback callback) {
        setDownloadStatus(MyOkhttp.DOWN_STATUS_ING);
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = mClient.newCall(request);
        call.enqueue(callback);
        return call;
    }

    public Call get(@NonNull String url, String header, @NonNull Callback callback) {
        setDownloadStatus(MyOkhttp.DOWN_STATUS_ING);
        Request.Builder request = new Request.Builder()
                .url(url)
                .get();

        if (header != null) {
            request.addHeader("Authorization", header);
        }

        Call call = mClient.newCall(request.build());
        call.enqueue(callback);
        return call;
    }

    public Call post(@NonNull String url, Map<String, Object> params, @NonNull Callback callback) {

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(params));

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Call call = mClient.newCall(request);
        call.enqueue(callback);
        return call;
    }

    public Call post(@NonNull String url, @NonNull Map<String, String> headerMap, Map<String, Object> params, @NonNull Callback callback) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(params));

        Request.Builder builder = new Request.Builder();
        builder.url(url).post(requestBody);
        if (headerMap != null) {
            for (String key : headerMap.keySet()) {
                builder.addHeader(key, headerMap.get(key));
            }
        }
        Request request = builder.build();
        Call call = mClient.newCall(request);
        call.enqueue(callback);
        return call;
    }
}
